/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gedigarr.grid;

import gedigarr.game.GameConstants;
import gedigarr.game.GameScore;
import gedigarr.objects.CharacterCell;
import gedigarr.resources.GameResource;
import gedigarr.resources.ResourceManager;
import java.awt.Color;
import java.awt.Font;

/**
 *
 * @author diiuser
 */
public class ScoreGrid extends TextGrid {

  private static final int SCORES_PER_PAGE = 5;
  private int page = 1;
  private final GameScore gameScores;
  private Color[] scoreColours = new Color[] {
    new Color( 0, 0, 50 ),
    new Color( 50, 0, 0 ),
    new Color( 0, 50, 0 ),
    new Color( 100, 100, 80 ),
    new Color( 170,170,170 ),
    Color.LIGHT_GRAY,
    Color.LIGHT_GRAY,
    Color.LIGHT_GRAY,
    Color.LIGHT_GRAY,
    Color.LIGHT_GRAY};
  GameResource<Font> gr = ResourceManager.getInstance().loadFont( "fonts/pooto2.ttf", ResourceManager.LOC_JAR );
  private Font fontTitle;

  public ScoreGrid( GameScore scores ) {
    this.fontTitle = gr.getResource().deriveFont( 20.0f );
    this.gameScores = scores;
    grid = new CharacterCell[GameConstants.GRID_HEIGHT][GameConstants.GRID_WIDTH];
    for ( int y = 0; y < GameConstants.GRID_HEIGHT; y++ ) {
      for ( int x = 0; x < GameConstants.GRID_WIDTH; x++ ) {
        grid[y][x] = new CharacterCell( false, false, Color.BLACK, "images/trans.png" );
        //grid[y][x].setActive( Cell.INACTIVE );
      }
    }
    refreshGrid();
    //▼►◄▲
  }

  public void refreshGrid() {
    drawTitle();
    int firstRow = 4;
    int rowInc = 3;

    GameScore.Score[] scores = gameScores.getHiScores();
    for ( int i = 0; i < 5; i++ ) {
      drawScore( scores[i], firstRow + ( rowInc * i ), scoreColours[i] );
    }
  }

  private void drawTitle() {
    setFont( new Font( "Courier New", Font.BOLD, 30 ) );
    //highlightRow( 1, Color.LIGHT_GRAY );
    drawString( "HIGH", Color.LIGHT_GRAY, 4, 1, GameConstants.GRID_WIDTH,
            GameConstants.GRID_HEIGHT, FLAG_HORIZONTAL | FLAG_CENTER );
    drawString( "SCORES", Color.LIGHT_GRAY, 4, 2, GameConstants.GRID_WIDTH,
            GameConstants.GRID_HEIGHT, FLAG_HORIZONTAL | FLAG_CENTER );


  }

  private void drawScore( GameScore.Score score, int row, Color bgColour ) {
    highlightRow( row, bgColour );
    highlightRow( row + 1, bgColour );
    setFont( new Font( "Times New Roman", Font.BOLD, 20 ) );
    drawString( score.name, Color.WHITE, 0, row, 5, 1, FLAG_HORIZONTAL );
    setFont( new Font( "Courier New", Font.BOLD, 15 ) );
    drawString( Integer.toString( score.level ), Color.WHITE, 6, row, 2, 1, FLAG_HORIZONTAL );
    drawString( Long.toString( score.score ), Color.WHITE, 1, row + 1, 7, 1, FLAG_HORIZONTAL );
  }

  private void highlightRow( int row, Color bgColour ) {
    for ( int x = 0; x < GameConstants.GRID_WIDTH; x++ ) {
      grid[row][x].setBGColour( bgColour );
      grid[row][x].setVisible( true );
    }
  }
}
